package leetcode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-27 11:16
 **/
public class LeetCode155 {

    public LeetCode155() {
        stack = new LinkedList<>();
        stack2 = new LinkedList<>();
    }

    //主栈
    Deque<Integer> stack = null;
    //辅助栈
    Deque<Integer> stack2 = null;

    public void push(int x) {
        stack.push(x);
        //在添加元素到主栈的同时，按非严格递减添加元素到stack2
        if (stack2.isEmpty() || x <= stack2.peek()) {
            stack2.push(x);
        }
    }

    public void pop() {
        Integer i = stack.pop();
        //查看删除元素是否为最小值，是则需要更新辅助栈
        if (!stack2.isEmpty() && stack2.peek().equals(i)) {
            stack2.pop();
        }
    }

    public int top() {
        //查看栈顶元素
        return stack.peek();
    }

    public int min() {
        //如果辅助栈不为空，辅助栈顶元素就是最小值
        if (!stack2.isEmpty()) {
            return stack2.peek();
        }
        //如果辅助栈为空，主栈栈顶元素为最小值
        return stack.peek();
    }

}
